<?php

final class Path
{
    static public function revise($path)
    {
        $prefix = "";
        if (preg_match("/^[\\.]{1,2}/", $path, $match)) {
            $prefix = $match[0];
        }
        return $prefix . preg_replace("/[\\.]*[\\/\\\\][\\/\\.\\\\]*/", "/", $path);
    }

    static public function directory($path)
    {
        $path = pathinfo($path, PATHINFO_DIRNAME);
        if (is_dir($path) == false && mkdir($path, 0777, true) == false) {
            return false;
        }
        return $path;
    }

    static public function extension($path)
    {
        return pathinfo($path, PATHINFO_EXTENSION);
    }

    static public function fileName($path)
    {
        return pathinfo($path, PATHINFO_FILENAME);
    }

    static public function fileMime($type)
    {
        $types = ["application", "image", "audio", "video", "text"];
        if (preg_match("/^([^\\/]+)\\//", $type, $match)) {
            return array_search($match[1], $types);
        }
        return 0;
    }
}